package com.qcby.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qcby.dao.UserDao;
import com.qcby.entity.PageResult;
import com.qcby.entity.Query.UserQuery;
import com.qcby.entity.Result;
import com.qcby.entity.User;
import com.qcby.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public PageResult<User> list(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getPage(), userQuery.getPageSize());
        List<User> page1 =  userDao.list(userQuery);
        System.out.println(page1);
        Page<User> page2 = (Page<User>) page1;
        PageResult<User> pageResult = new PageResult<>(page2.getTotal(),page2.getResult());
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteById(Integer id) {
        userDao.deleteById(id);
        userDao.deleteCheckId(id);
        userDao.deleteLikeId(id);
    }

    @Override
    public User findById(Integer id) {
        User user = userDao.findById(id);
        return user;
    }

    @Override
    public void updateById(User user) {
        userDao.updateById(user);
    }

    @Override
    public void add(User user) {
        userDao.add(user);
    }
    @Override
    public List<User> UserLogin(String username, String password) {
        return userDao.UserLogin(username,password);
    }

    @Override
    public int register(User user) {
        System.out.println("Impl层:"+user.getUsername());
        System.out.println("Impl层:"+user.getPassword());
        return userDao.register(user);
    }

    @Override
    public int isOwner(Integer isOwner) {
        return userDao.isOwner(isOwner);
    }

    @Override
    public User findByName(String username) {
        return userDao.findByName(username);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Result updatePassword(String password, String account, String newPassword) {
        User user = userDao.findPassword(account,password);
        if(user==null)
            return Result.error("密码错误");
        userDao.updatePassword(newPassword,account);
        return Result.success();
    }

    @Override
    public Integer selectMaxId() {
        return userDao.selectMaxId();
    }
}
